//
//  SearchListController.m
//  Ten
//
//  Created by anfuer on 2018/11/5.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "SearchListController.h"
#import "SPPageMenu.h"
#import "companyCell.h"
#import "HomeTbvCell.h"
@interface SearchListController ()<SPPageMenuDelegate, UIScrollViewDelegate>
{
    NSMutableArray*mary1;
    NSMutableArray*mary2;
}
@property (weak, nonatomic) IBOutlet UIView *MuneBack;
@property (weak, nonatomic) IBOutlet UIScrollView *scrview;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrW;
@property (weak, nonatomic) IBOutlet UITableView *tbv1;
@property (weak, nonatomic) IBOutlet UITableView *tbv2;
@property (weak, nonatomic) IBOutlet UIView *hidenView1;
@property (weak, nonatomic) IBOutlet UIView *hidenView2;
@property (weak, nonatomic) IBOutlet UISearchBar *seacBar;
@property (nonatomic, weak) SPPageMenu *pageMenu;
@end

@implementation SearchListController

- (void)viewDidLoad {
    [super viewDidLoad];
    _seacBar.text=_PassKeyWork;
    _tbv1.separatorColor=[UIColor clearColor];
    _tbv2.separatorColor=[UIColor clearColor];
    _scrW.constant=DEVW*2;
    
    SPPageMenu *pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, DEVW, 50) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
    // 传递数组，默认选中第2个
    [pageMenu setItems:@[@"职位",@"公司"] selectedItemIndex:0];
    pageMenu.permutationWay=SPPageMenuPermutationWayNotScrollEqualWidths;
    // 设置代理
    pageMenu.delegate = self;
    [_MuneBack addSubview:pageMenu];
    _pageMenu = pageMenu;
    _tbv1.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        mary1=[NSMutableArray array];
        [self loadData];
    }];
    _tbv2.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        mary2=[NSMutableArray array];
        [self loadData];
    }];
    
}

-(void)loadData
{
    [FuncPublic getData:self parameters:@{@"keywords":_seacBar.text} RequestUrl:Csearch success:^(NSDictionary *acountInfo) {
        if (_pageMenu.selectedItemIndex==0) {
            mary1=[NSMutableArray arrayWithArray:acountInfo[@"jobs"]];
            [_tbv1 reloadData];
        }else
        {
            
        }
        NSLog(@"%@",acountInfo);
    }];

//
    
}

#pragma mark - scrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView==_scrview) {
        [self.pageMenu moveTrackerFollowScrollView:scrollView];
    }
    
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    if (labs(toIndex - fromIndex) >= 2) {
        [_scrview setContentOffset:CGPointMake(DEVW * toIndex, 0) animated:NO];
    } else {
        [_scrview setContentOffset:CGPointMake(DEVW * toIndex, 0) animated:YES];
    }
    mary1=[NSMutableArray array];
    mary2=[NSMutableArray array];
    [self loadData];
}
- (IBAction)back:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  _pageMenu.selectedItemIndex==0?mary1.count:mary2.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if (_pageMenu.selectedItemIndex==0) {
        HomeTbvCell*cell=[HomeTbvCell CellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell Cellinit:mary1[indexPath.row]];
        if (_seacBar.text.length>1) {
            cell.lb1.attributedText=[FuncPublic setSearchResultStringColor:cell.lb1.text AndSearchstr:_seacBar.text];
        }
        return cell;
    }else
    {
        companyCell*cell=[companyCell CellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
}


#pragma mark 设置每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _pageMenu.selectedItemIndex==0? [HomeTbvCell FSCellHeightForTableView:tableView indexPath:indexPath cacheKey:@"HomeTbvCell" cellContentViewWidth:0 bottomOffset:0]:80;
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (tableView==_tbv1) {
        
        [MyUtils collviewSelidex:@"职位详情" toVc:self andShopInfo:mary1[indexPath.row]];
    }else
    {
        [MyUtils collviewSelidex:@"公司详情" toVc:self andShopInfo:mary2[indexPath.row]];
    }
}

//点击键盘上的search按钮时调用

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar

{
    if ([searchBar.text empty]) {
        return;
    }
    [searchBar resignFirstResponder];
    [self loadData];
}

//输入文本实时更新时调用

- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

{
    if ([searchText empty]) {
        return;
    }
   [self loadData];
}

@end
